﻿/*FOXPHP上传附件*/
function RemoveCompleteFile(id){
	DeleteUndefinedFile(id);
	$("#File"+id).remove();
}

$(function(){
	 //获取未定义附件
			   $.getJSON(upload_undefinedfile, function(json){
				   if(json.status=='ok'){
						 $("#UploadList").show(500);
					  	 $.each(json.data, function(key,file) {
						    var removefile=$('<td class="text-right"><a class="btn btn-darkorange btn-sm" href="javascript:RemoveCompleteFile('+file.fid+');">'+upload_removefiletitle+'</a></td>');
							var filesize=file.filesize/1024;
							$("#UploadList tbody").append($('<tr id="File'+file.fid+'"><td>'+file.filename+'</td><td>'+filesize.toFixed(2)+'k</td><td>'+upload_completetitle+'</td></tr>').append(removefile));
						});
				   }
				});
			  
			  //创建
                var uploader = WebUploader.create({
					
            		auto:true,
					
                    swf: upload_swf,
                
                    server: upload_server,
            
                    pick: upload_id,
                    
                    fileNumLimit:upload_filenumlimit,
                    
                    fileSizeLimit:upload_filesizelimit,
                    
                    fileSingleSizeLimit:upload_filesinglesizelimit,
					
					formData:{SecurityForm:SecurityForm},
            
                    accept: {
                       title: upload_extensions_title,
                       extensions: upload_extensions,
                       mimeTypes: upload_mimetypes
                    }
                    
                    
                });
				
				uploader.on('fileQueued', function( file ) {
					$("#UploadList").show(500);
					var removefile=$('<td class="text-right"><a class="btn btn-darkorange btn-sm removefile" href="javascript:void(0);" id="0">'+upload_removefiletitle+'</a></td>');
					var filesize=file.size/1024;
					
					var fileinfo=$('<tr id="'+file.id+'"><td>'+file.name+'</td><td>'+filesize.toFixed(2)+'k</td><td id="UploadStats'+file.id+'"></td></tr>').append(removefile);
                     //alert(file.id+' '+file.name);
					 $("#UploadList tbody").append(fileinfo);

					 removefile.on( 'click','.removefile', function() {
						 	var id=$(this).attr('id');
							if(id>0){
								$("#UploadStats"+file.id).html(upload_deletefiletitle);
								//$.post(upload_delete,{id:id}, function(json) {return true;});
								DeleteUndefinedFile(id);
							}
						 	uploader.removeFile(file.id,true);
							$(this).parents("tr").remove();
					 });
					 
                });
				
				uploader.on('uploadStart', function( file ) {
					$("#UploadStats"+file.id).html(upload_filetitle);
				});
				
				uploader.on('uploadAccept', function(object, response) {
					if(response.status==='ok'){
						$("#UploadStats"+object.file.id).parents("tr").find('.removefile').attr('id',response.id);
						$("#UploadStats"+object.file.id).html('<i class="fa fa-check-circle"></i>'+response.message);
					}else{
						$("#UploadStats"+object.file.id).html('<i class="fa fa-times-circle"></i>'+response.message);
					}
				});
				
 });

